<template>
  <el-form ref="form" :model="loginForm" size="large" class="formInput">
    <el-form-item>
      <el-button @click="login" type="primary">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// import HelloWorld from './components/HelloWorld.vue'
import security from '@/utils/securityPlugin.js';
import JITHTTPCLIENT from '@/utils/JITHTTPCLIENT_21121.js';
import { initParam } from '@/utils/initxml';
import axios from 'axios';

export default {
  name: 'App',
  data() {
    return {
      cert: [],
      pin: '',
      form: {
        pin: '',
        certId: '',
        origion: 'This is a test',
        certstr: '',
        changedata: '',
      },
      loginFlag: false,
    };
  },
  methods: {
    login(){
      axios.get(window.apiUrl.VITE_SERVER_URL+'/public/user/generateRandom')
          .then(response => {
            console.log(response.data);
            this.attachSign(response.data.data);
          })
          .catch(error => {
            console.error('There was an error!', error);
          });
    },
    attachSign(randomNumber) {
      var ret = -1;
      var message = '';
      if (randomNumber) {
        var InitParam2 = initParam;

        JITHTTPCLIENT.RegisterParamMore(InitParam2);
        if (JITHTTPCLIENT.GetErrorCode() != 0) {
          alert('初始化失败，错误码：' + JITHTTPCLIENT.GetErrorCode() + ' 错误信息：' + JITHTTPCLIENT.GetErrorMessage(JITHTTPCLIENT.GetErrorCode()));
          return;
        }
        //
        ret = JITHTTPCLIENT.SetCertChooseType(0);
        if (ret != 0) {
          ret = JITHTTPCLIENT.GetErrorCode();
          if (ret != 0) {
            message = JITHTTPCLIENT.GetErrorMessage(ret);
            alert('设置证书选择类型失败，错误码：' + ret + ' 错误信息：' + message);
          } else {
            alert('请先进行初始化操作');
          }
          return;
        }
        //
        ret = JITHTTPCLIENT.SetCert('SC', '', '', '', '', '');
        if (ret != 0) {
          ret = JITHTTPCLIENT.GetErrorCode();
          if (ret != 0) {
            message = JITHTTPCLIENT.GetErrorMessage(ret);
            alert('设置证书过滤失败，错误码：' + ret + ' 错误信息：' + message);
          }
          return;
        }

        var count = 0;
        var item;
        var obj = '1';
        for (var i = 0; i < obj.length; i++) { //遍历Radio
          if (obj[i].checked) {
            item = Number(obj[i].value);
          }
        }
        JITHTTPCLIENT.SetFileNameUseUTF8(item);

        //
        message = JITHTTPCLIENT.AttachSignStr('', randomNumber);
        ret = JITHTTPCLIENT.GetErrorCode();
        count++;
        if (ret != 0) {
          message = JITHTTPCLIENT.GetErrorMessage(ret);
          alert('签名失败，错误码：' + ret + ' 错误信息：' + message);
        } else {
          console.log('签名信息:', message);
          //调用解密
          this.detachSign(message,randomNumber);
        }
        return;
      } else {
        alert('输入项为空');
      }
    },
    detachSign(message,randomNumber) {
      /*此处为vctk—demo使用，客户接入时直接定义initParam即可*/
      var InitParam2 = initParam;
      /*应用接入从此处开始*/
      if (message) {
        /*此处为vctk—demo使用，客户接入时直接定义initParam即可*/
        axios.post(window.apiUrl.VITE_SERVER_URL+'/public/user/detachSign',{signData:message,plain:randomNumber})
            .then(response => {
              console.log(response.data);
              this.$emit('yfCALoginCheck', response.data.data);
            })
            .catch(error => {
              console.error('There was an error!', error);
            });

        //this.$emit('yfCALoginCheck', data);

      } else {
        alert('输入项为空');
      }

    }
  },
};
</script>

<style>
</style>
